package o;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnAttachStateChangeListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;
import com.huawei.appgallery.forum.section.view.widget.tips.BubbleLayout;
import java.lang.ref.WeakReference;
import o.cab.i;

public class cbe {
    private int ʻ;
    private b ʻॱ;
    private int ʼ;
    private BubbleLayout ʽ;
    private String ˊ;
    private boolean ˊॱ;
    private String ˋ;
    private int ˋॱ;
    private PopupWindow ˎ;
    private WeakReference<Activity> ˏ;
    private boolean ˏॱ;
    private d ͺ;
    private TextView ॱ;
    private int ॱˊ;
    private int[] ॱॱ;
    private boolean ॱᐝ;
    private int ᐝ;

    protected class a implements OnAttachStateChangeListener {
        final /* synthetic */ cbe ˊ;

        protected a(cbe o_cbe) {
            this.ˊ = o_cbe;
        }

        public void onViewAttachedToWindow(View view) {
        }

        public void onViewDetachedFromWindow(View view) {
            if (this.ˊ.ˏ != null) {
                Activity activity = (Activity) this.ˊ.ˏ.get();
                if (activity != null) {
                    bqx.ˏ("BubbleTips", "onViewDetachedFromWindow: activity.isChangingConfigurations()=" + activity.isChangingConfigurations() + ", activity.isFinishing()=" + activity.isFinishing());
                    if (activity.isChangingConfigurations() || activity.isFinishing()) {
                        this.ˊ.ˏ();
                    }
                }
            }
        }
    }

    static class b extends Handler {
        private WeakReference<cbe> ˎ;

        private b(cbe o_cbe) {
            this.ˎ = new WeakReference(o_cbe);
        }

        public void handleMessage(Message message) {
            if (message != null && this.ˎ != null) {
                cbe o_cbe = (cbe) this.ˎ.get();
                if (o_cbe != null && message.what == 1001) {
                    o_cbe.ˊ(o_cbe.ॱ());
                    o_cbe.ॱ(o_cbe.ॱ());
                    if (o_cbe.ͺ != null) {
                        o_cbe.ͺ.ॱ();
                    }
                }
            }
        }
    }

    public static final class c {
        private Activity ˋ;
        private String ˎ;
        private String ॱ;

        public c ˏ(Activity activity) {
            this.ˋ = activity;
            return this;
        }

        public c ˏ(String str) {
            this.ॱ = str;
            return this;
        }

        public c ˋ(String str) {
            this.ˎ = str;
            return this;
        }

        public cbe ˊ() {
            return new cbe(this.ˋ, this.ॱ, this.ˎ);
        }
    }

    static class d implements OnGlobalLayoutListener {
        private final View ˊ;
        private final int ˋ;
        private final int ˎ;
        private final int ˏ;
        private cbe ॱ;

        private d(cbe o_cbe, View view, int i, int i2, int i3) {
            this.ॱ = o_cbe;
            this.ˊ = view;
            this.ˏ = i;
            this.ˋ = i2;
            this.ˎ = i3;
        }

        public void onGlobalLayout() {
            if (this.ˊ != null) {
                this.ˊ.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                if (this.ॱ != null) {
                    this.ॱ.ᐝ = this.ˊ.getWidth();
                    this.ॱ.ʻ = this.ˊ.getHeight();
                    bqx.ˏ("BubbleTips", "AnchorView onGlobalLayout: mWidth=" + this.ॱ.ᐝ + ", mHeight=" + this.ॱ.ʻ);
                    this.ॱ.ॱ(this.ˊ, this.ˏ, this.ˋ, this.ˎ);
                }
            }
        }

        private void ॱ() {
            if (this.ˊ != null && this.ˊ.getViewTreeObserver() != null && this.ˊ.getViewTreeObserver().isAlive()) {
                this.ˊ.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        }
    }

    static class e implements OnGlobalLayoutListener {
        private WeakReference<cbe> ˊ;
        private View ˋ;
        private int ˎ;
        private int ˏ;
        private int ॱ;

        private e(cbe o_cbe, int i, int i2, View view, int i3) {
            this.ˊ = new WeakReference(o_cbe);
            this.ˎ = i;
            this.ॱ = i2;
            this.ˋ = view;
            this.ˏ = i3;
        }

        public void onGlobalLayout() {
            cbe o_cbe = (cbe) this.ˊ.get();
            if (o_cbe != null && o_cbe.ʽ != null && o_cbe.ˏ != null) {
                o_cbe.ʽ.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                int width = o_cbe.ʽ.getWidth();
                int height = o_cbe.ʽ.getHeight();
                bqx.ˏ("BubbleTips", "ContentView onGlobalLayout: mMeasuredWidth=" + width + ", mMeasuredHeight=" + height);
                if (o_cbe.ॱˊ == 0) {
                    o_cbe.ʽ.setTriangleOffset(o_cbe.ʼ / 2);
                }
                if (o_cbe.ʼ != width || o_cbe.ˋॱ != height) {
                    o_cbe.ॱ(this.ˋ, this.ˏ, this.ˎ, this.ॱ);
                }
            }
        }
    }

    class j implements OnDismissListener {
        final /* synthetic */ cbe ॱ;

        private j(cbe o_cbe) {
            this.ॱ = o_cbe;
        }

        public void onDismiss() {
            if (this.ॱ.ˏ != null) {
                Activity activity = (Activity) this.ॱ.ˏ.get();
                if (activity != null) {
                    bqx.ˏ("BubbleTips", "this tips is dismiss: " + this.ॱ.ˋ);
                    if (!this.ॱ.ˏॱ && this.ॱ.ॱ()) {
                        this.ॱ.ॱ(true);
                    }
                    if (!this.ॱ.ˊ() && this.ॱ.ॱ() && activity.isChangingConfigurations()) {
                        this.ॱ.ॱ(true);
                    }
                    if (!this.ॱ.ˊ() && this.ॱ.ॱ() && activity.isFinishing()) {
                        this.ॱ.ॱ(true);
                    }
                    if (this.ॱ.ʽ != null) {
                        this.ॱ.ʽ = null;
                    }
                    if (this.ॱ.ॱ != null) {
                        this.ॱ.ॱ = null;
                    }
                    if (this.ॱ.ʻॱ != null) {
                        this.ॱ.ʻॱ.removeMessages(1001);
                        this.ॱ.ʻॱ = null;
                    }
                    if (this.ॱ.ͺ != null) {
                        this.ॱ.ͺ.ॱ();
                        this.ॱ.ͺ = null;
                    }
                }
            }
        }
    }

    private cbe(Activity activity, String str, String str2) {
        this.ˏॱ = false;
        this.ॱᐝ = MinimumFactory.getBoolValue(ContentManager.getInstant().getContext());
        this.ˏ = new WeakReference(activity);
        this.ˋ = str;
        this.ˊ = str2;
        this.ʻॱ = new b();
        this.ॱॱ = new int[2];
        ˎ();
    }

    private void ˎ() {
        if (this.ˏ != null) {
            Context context = (Activity) this.ˏ.get();
            if (context != null && !ˊ()) {
                this.ˎ = new PopupWindow(context);
                this.ʽ = ॱ((Activity) context);
                this.ॱ = (TextView) this.ʽ.findViewById(o.cab.e.ﹳ);
                this.ॱ.setMaxWidth(((azb.ॱ(context) * 2) / 3) - context.getResources().getDimensionPixelSize(o.cab.c.ॱ));
                if (!TextUtils.isEmpty(this.ˊ)) {
                    this.ॱ.setText(this.ˊ);
                }
                this.ˎ.setContentView(this.ʽ);
                this.ˎ.setWidth(-2);
                this.ˎ.setHeight(-2);
                this.ˎ.setBackgroundDrawable(new ColorDrawable(context.getResources().getColor(o.cab.b.ʽ)));
                this.ˎ.setOutsideTouchable(true);
                this.ˎ.setFocusable(false);
                this.ˎ.setAnimationStyle(0);
                this.ˎ.setOnDismissListener(new j());
            }
        }
    }

    public void ˊ(View view, int i, int i2, int i3) {
        if (this.ˏ != null && ((Activity) this.ˏ.get()) != null && !ˊ() && view != null) {
            this.ʽ.addOnAttachStateChangeListener(new a(this));
            ˋ(i3);
            this.ʽ.measure(0, 0);
            this.ʼ = this.ʽ.getMeasuredWidth();
            this.ˋॱ = this.ʽ.getMeasuredHeight();
            this.ʽ.getViewTreeObserver().addOnGlobalLayoutListener(new e(i, i2, view, i3));
            this.ͺ = new d(view, i3, i, i2);
            view.getViewTreeObserver().addOnGlobalLayoutListener(this.ͺ);
            ˎ(true);
        }
    }

    public void ˎ(boolean z) {
        this.ˊॱ = z;
    }

    public boolean ॱ() {
        return this.ˊॱ;
    }

    private void ˊ(boolean z) {
        if (this.ʽ != null) {
            this.ʽ.setVisibility(z ? 0 : 4);
        }
    }

    public BubbleLayout ॱ(Activity activity) {
        if (this.ʽ == null) {
            this.ʽ = (BubbleLayout) LayoutInflater.from(activity).inflate(i.ʿ, null);
        }
        return this.ʽ;
    }

    private void ˏ() {
        this.ˏॱ = true;
        if (this.ˎ != null) {
            this.ˎ.dismiss();
            this.ˎ = null;
        }
        if (this.ʽ != null) {
            this.ʽ = null;
        }
        if (this.ॱ != null) {
            this.ॱ = null;
        }
        if (this.ʻॱ != null) {
            this.ʻॱ.removeMessages(1001);
            this.ʻॱ = null;
        }
        if (this.ͺ != null) {
            this.ͺ.ॱ();
            this.ͺ = null;
        }
    }

    private void ॱ(View view, int i, int i2, int i3) {
        view.getLocationOnScreen(this.ॱॱ);
        int[] iArr = new int[2];
        boolean ˎ = ˎ(i, iArr, i2, i3);
        bqx.ˏ("BubbleTips", "showAtLocation: offset[0]=" + iArr[0] + ", offset[1]=" + iArr[1]);
        if (!ˎ || this.ˎ == null) {
            bqx.ॱ("BubbleTips", "showAtLocation: get offset failed!");
            return;
        }
        if (this.ˎ.isShowing()) {
            this.ˎ.update(view, iArr[0], iArr[1], this.ʼ, this.ˋॱ);
        } else {
            this.ˎ.showAsDropDown(view, iArr[0], iArr[1]);
        }
        if (this.ʻॱ != null) {
            ˊ(ॱ());
            this.ʻॱ.sendMessageDelayed(this.ʻॱ.obtainMessage(1001), 250);
        }
    }

    private boolean ˎ(int i, int[] iArr, int i2, int i3) {
        switch (i) {
            case 48:
                return ˊ(iArr, i2, i3);
            case 80:
                return ˏ(iArr, i2, i3);
            default:
                return false;
        }
    }

    private boolean ˊ(int[] iArr, int i, int i2) {
        iArr[0] = 0;
        iArr[1] = 0;
        if (this.ॱॱ[1] < this.ˋॱ) {
            return false;
        }
        if (i == 0 && i2 == 0) {
            iArr[0] = (this.ᐝ - this.ʼ) / 2;
            iArr[1] = -(this.ˋॱ + this.ʻ);
            return true;
        } else if (this.ॱᐝ) {
            iArr[0] = (this.ᐝ - this.ʼ) - i;
            iArr[1] = -((this.ˋॱ + this.ʻ) + i2);
            return true;
        } else {
            iArr[0] = i;
            iArr[1] = -((this.ˋॱ + this.ʻ) + i2);
            return true;
        }
    }

    private boolean ˏ(int[] iArr, int i, int i2) {
        iArr[0] = 0;
        iArr[1] = 0;
        if (this.ˏ == null) {
            return false;
        }
        Activity activity = (Activity) this.ˏ.get();
        if (activity == null) {
            return false;
        }
        if ((epv.ॱ(activity) - this.ॱॱ[1]) - this.ʻ < this.ˋॱ) {
            return false;
        }
        if (i == 0 && i2 == 0) {
            iArr[0] = (this.ᐝ - this.ʼ) / 2;
            iArr[1] = 0;
            return true;
        }
        if (this.ॱᐝ) {
            iArr[0] = (this.ᐝ - this.ʼ) - i;
            iArr[1] = i2;
        } else {
            iArr[0] = i;
            iArr[1] = i2;
        }
        return true;
    }

    private void ˋ(int i) {
        if (this.ʽ != null) {
            switch (i) {
                case 3:
                    this.ʽ.setDirection(3);
                    return;
                case 5:
                    this.ʽ.setDirection(1);
                    return;
                case 48:
                    this.ʽ.setDirection(4);
                    return;
                case 80:
                    this.ʽ.setDirection(2);
                    return;
                default:
                    return;
            }
        }
    }

    public void ˊ(int i) {
        if (this.ʽ != null) {
            this.ॱˊ = i;
            this.ʽ.setTriangleOffset(i);
        }
    }

    private void ॱ(boolean z) {
        bsc.ˋ().ˋ(this.ˋ, z);
    }

    private boolean ˊ() {
        return bsc.ˋ().ˏ(this.ˋ, false);
    }
}
